Key knowledge
Key skills
A Heuristic is an additional piece of information that can be used solve a problem.
What information could we use to make our search more efficient?
The actual distance from the current node to the goal.
The Manhattan distance (grid distance - total x and y)
A* is a search algorithm to find the shortest path from a start node to a goal node.
It combines Dijkstra’s and a Heuristic
Dijkstra’s explores by actual path cost so far (\(g(n)\))
A* uses the actual cost + heuristic cost (\(f(n)=g(n)+h(n)\))
A*: f(n) = g(n) + h(n)
where:
A* is greedy by f(n)
A*: f(n) = g(n) + h(n)
where:
Run A* on the given graph. Mark the nodes explored by A*.
Modify Dijkstra’s to use A*
Assume you have a list h that contains the heuristic values for each node.
e.g h[A] = 4, h[B] = 2, h[C] = 0
procedure A*(G, s, t, h):
g[v] ← ∞ ; parent[v] ← NIL for all v
g[s] ← 0
Q ← {(s, h(s))} # min-priority by f = g + h
while Q not empty:
u ← extract_min_f(Q)
if u = t: break # stop early if goal reached
for (v,w) in Adj[u]:
tent ← g[u] + w
if tent < g[v]:
g[v] ← tent ; parent[v] ← u
update(Q, v, g[v] + h(v))
return g, parent # g[t] is shortest if h is admissibleQ1. Consider the graph below. The source is S, the goal is G. The numbers on edges are weights, and the values in brackets are heuristic estimates h(n) from each node to the goal.
(h=7)
S ——3—— A ——4—— G(h=0)
\ |
2 5
\ |
B(h=6)
At the start of A*, which node will be chosen first after S is expanded?
A. A B. B C. G D. None — the search terminates at S
Q2. A* differs from Dijkstra’s algorithm because it A. only works on unweighted graphs. B. uses a heuristic to estimate distance to the goal. C. guarantees optimality even if the heuristic overestimates. D. ignores the actual distance from the source.
Q3. If the heuristic function h(n) in A* is admissible, then A. A* is guaranteed to find an optimal path. B. A* may fail to find a path if one exists. C. A* will always expand fewer nodes than Dijkstra’s. D. A* cannot be used on weighted graphs.
Q4. Which of the following is a suitable heuristic h(n) for pathfinding in a 2D grid where movement is restricted to horizontal and vertical steps?
A. Straight-line (Euclidean) distance B. Manhattan (grid) distance C. Random values D. Weighted average of past path costs
Q5. (3 marks) Explain why A* reduces to Dijkstra’s algorithm when the heuristic function h(n) = 0 for all nodes.
Q6. (4 marks) Describe the conditions that a heuristic function must satisfy to guarantee that A* finds the shortest path. Give one example of a heuristic that satisfies these conditions in a grid navigation problem.
Q7. (5 marks) A* uses the formula f(n) = g(n) + h(n).
Why Heuristics?
Example – Exam Timetabling
Example – Delivery Routes
Example – Job Scheduling
Suggest suitable heuristics for the following NP-hard problems:
An algorithmic technique that relies solely on local heuristics.
Imagine finding the highest peak in a hilly terrain by always moving uphill.
Hill climbing is a metaphor, you can use other heuristics
Example:
Hill Climbing can be used to search through a solution space
Neighbors are similar solutions, typically differing by a small change.
Steepest-Ascent: check all neighbours, move to the best one
Stochastic: pick a random neighbour, move if it improves
First-Choice: scan neighbours in given order, stop at the first improvement
All are greedy: only move if the heuristic improves
“Sometimes you have to take two steps back to take ten forward.”
Nipsey Hussle
Annealing - heat treating metal to remove defects
Start with an initial solution.
At each step:
Pick a neighbour solution.
If it’s better → move there.
If it’s worse → maybe still move there with probability:
\[ P = e^{-\Delta E / T} \]
where \(\Delta E\) = how much worse, \(T\) = current “temperature.”
Gradually decrease temperature.
Stop when system is “frozen.”
Start with a random tour of cities.
Swap two cities:
Early on: lots of exploration.
Later: fine-tuning near the best tours.